// pages/login/login.js
import { toast } from '@/utils/extendApi'
import { getLoginCode, getUserInfo } from '@/api/user'
import { setStorage } from '@/utils/storage'
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
import { userStore } from '@/store/userStore'
import { debounce } from 'miniprogram-licia'
ComponentWithStore({
  storeBindings: {
    store: userStore,
    fields: ['token','userInfo'],
    actions: ['setToken','setUserInfo']
  },
  methods: {
    login: debounce(function(){
      wx.login({
        success: async ({code})=>{
          if(code){
            const {data:{token}} = await getLoginCode(code)
            setStorage('token',token)
            this.setToken(token)
            this.getInfo()
            wx.navigateBack()
          }
          else{
            toast({title:"授权失败"})
          }
        }
      })
    },500),
    async getInfo(){
      const { data } = await getUserInfo()
      setStorage("UserInfo",data)
      this.setUserInfo(data)
    }
  }
})
